package com.xnyzc.lhy.resource.service.impl.invoice;

import com.xnyzc.lhy.common.constant.InvoiceAaUrlConstant;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.RequestMethodUtil;
import com.xnyzc.lhy.common.util.sign.InvoiceAaSign;
import com.xnyzc.lhy.resource.entity.invoice.InvoiceAaOpenParam;
import com.xnyzc.lhy.resource.entity.invoice.InvoiceAaOpenResult;
import com.xnyzc.lhy.resource.entity.invoice.InvoiceAaQueryOrCancellationParam;
import com.xnyzc.lhy.resource.service.invoice.IInvoiceAaService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电子发票服务(Aa)
 * </p>
 *
 * @author 刘举
 * @since 2019-10-24
 */
@Service
@Slf4j
public class InvoiceAaServiceImpl implements IInvoiceAaService {


    @Value("${global.invoiceAa.url}")
    private String request_url;

    /**
     * 发票开具
     *
     * @param invoiceAaParam
     * @return
     */
    @Override
    public InvoiceAaOpenResult invoiceAaOpenRequest(InvoiceAaOpenParam invoiceAaParam) {
        InvoiceAaOpenResult invoiceAaOpenResult = new InvoiceAaOpenResult();

        // 如果收件人手机号不存在，写死其他手机号
        if (CheckUtil.strIsEmpty(invoiceAaParam.getReceive_mobile())) {
            invoiceAaParam.setReceive_mobile("17844444444");
        }

        // 加签
        invoiceAaParam.setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));
        String sign = InvoiceAaSign.getSign(invoiceAaParam);
        invoiceAaParam.setSign(sign);
        // 请求发票接口
        String data = RequestMethodUtil.requestMethodJson(JSONObject.fromObject(invoiceAaParam), request_url + InvoiceAaUrlConstant.CREATE_URL);
        log.info("发票开具请求返回信息,{}", data);
        if (CheckUtil.strIsNotEmpty(data)) {
            invoiceAaOpenResult = com.alibaba.fastjson.JSONObject.parseObject(data, InvoiceAaOpenResult.class);
        } else {
            log.info("发票开具请求失败");
        }
        return invoiceAaOpenResult;
    }


    /**
     * 发票查询
     *
     * @param invoiceQueryParam
     * @return
     */
    @Override
    public InvoiceAaOpenResult invoiceAaQueryRequest(InvoiceAaQueryOrCancellationParam invoiceQueryParam) {
        InvoiceAaOpenResult invoiceAaOpenResult = new InvoiceAaOpenResult();
        // 加签
        invoiceQueryParam.setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));
        String sign = InvoiceAaSign.getSign(invoiceQueryParam);
        invoiceQueryParam.setSign(sign);
        // 请求发票接口
        String data = RequestMethodUtil.requestMethodJson(JSONObject.fromObject(invoiceQueryParam), request_url + InvoiceAaUrlConstant.QUERY_URL);
        log.info("发票查询请求返回信息,{}", data);
        if (CheckUtil.strIsNotEmpty(data)) {
            invoiceAaOpenResult = com.alibaba.fastjson.JSONObject.parseObject(data, InvoiceAaOpenResult.class);
        } else {
            log.info("发票查询请求失败");
        }
        return invoiceAaOpenResult;
    }

    /**
     * 发票冲红
     *
     * @param invoiceCancellationParam
     * @return
     */
    @Override
    public InvoiceAaOpenResult invoiceAaCancellationRequest(InvoiceAaQueryOrCancellationParam invoiceCancellationParam) {
        InvoiceAaOpenResult invoiceAaOpenResult = new InvoiceAaOpenResult();
        // 加签
        invoiceCancellationParam.setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));
        String sign = InvoiceAaSign.getSign(invoiceCancellationParam);
        invoiceCancellationParam.setSign(sign);
        // 请求发票接口
        String data = RequestMethodUtil.requestMethodJson(JSONObject.fromObject(invoiceCancellationParam), request_url + InvoiceAaUrlConstant.REVERSE_URL);
        log.info("发票冲红请求返回信息,{}", data);
        if (CheckUtil.strIsNotEmpty(data)) {
            invoiceAaOpenResult = com.alibaba.fastjson.JSONObject.parseObject(data, InvoiceAaOpenResult.class);
        } else {
            log.info("发票冲红请求失败");
        }
        return invoiceAaOpenResult;
    }


}
